package com.atguigu.tingshu.search.service.impl;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.config.ThreadPoolExecutorConfiguration;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;

import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.vo.search.AlbumInfoDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public AlbumInfoDetailVo getItem(Long albumId) {

        // 创建AlbumInfoDetailVo对象，封装响应结果数据
        AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();

        CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> { // 远程调用：根据专辑的id查询专辑基本数据
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfoById(albumId);
            AlbumInfo albumInfo = albumInfoResult.getData();
            albumInfoDetailVo.setAlbumInfo(albumInfo);

            return albumInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {  // 远程调用：根据专辑的id查询专辑统计数据
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.findAlbumStatByAlbumId(albumId);
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVo);
        }, threadPoolExecutor);

        CompletableFuture<Void> cf3 = cf1.thenAcceptAsync(albumInfo -> {  // 远程调用：根据三级分类的id查询分类数据
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.findCategoryByC3Id(albumInfo.getCategory3Id());
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            albumInfoDetailVo.setBaseCategoryView(baseCategoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> cf4 = cf1.thenAcceptAsync(albumInfo -> {
            // 远程调用：根据用户的id查询主播数据
            Result<UserInfo> userInfoResult = userInfoFeignClient.findByUserId(albumInfo.getUserId());
            UserInfo userInfo = userInfoResult.getData();
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            albumInfoDetailVo.setAnnouncer(userInfoVo);

        }, threadPoolExecutor);

        CompletableFuture.allOf(cf1, cf2, cf3, cf4).join();
        return albumInfoDetailVo;
    }
}